<?php
class longController extends appController
{
    //奖品页面
    public function prize()
    {
         
         
        $page = yf_empty($_REQUEST['page'], 1, true);
         
        $prize_name = $_REQUEST['k'];
         
        $list = Model('long')->get_prize($prize_name,$page);
         
        $this->assign('result',$list['data']);
        $this->assign('user_list_page',$list['pagelink']);
        $this->display('prize.html');
    }
    //增加奖品
    public function add_prize()
    {
        $this->display('add_prize.html');
    }
    //奖品时段页面
    public function rule()
    {
        $page = yf_empty($_REQUEST['page'], 1, true);
         
        $prize_name = $_REQUEST['k'];
        $id = $_REQUEST['id'];
         
        $list = Model('long')->get_rule($prize_name,$id,$page);
        if(!empty($list['data'])){
            foreach($list['data'] as $k => $v)
            {
                $list['data'][$k]['begintime'] = date('Y-m-d H:i:s',$v['begintime']);
                $list['data'][$k]['endtime'] = date('Y-m-d H:i:s',$v['endtime']);
            }
        }
        
         
        $this->assign('result',$list['data']);
        $this->assign('user_list_page',$list['pagelink']);
        $this->display('rule.html');
    }
    //增加奖品时段
    public function add_rule()
    {
        $prize_name = Model('long')->get_prize_name();
         
        $this->assign('prize_name',$prize_name);
        $this->display('add_rule.html');
    }
    //中奖用户页面
    public function win_users()
    {
        $page = $_REQUEST['page']>0?$_REQUEST['page']:1;
         
        $prize_name = $_REQUEST['k'];
         
        $list = Model('long')->get_win_users($prize_name,$page);
        if(!empty($list['data'])){
            foreach($list['data'] as $k => $v)
            {
                $list['data'][$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            }
        }
        $this->assign('result',$list['data']);
        $this->assign('user_list_page',$list['pagelink']);
        $this->display('win_users.html');
    }
    //修改奖品信息
    public function update_prize()
    {
        $id = $_GET['id'];
        $res = Model('long')->get_prize_one($id);
        $this->assign("res",$res);
        $this->display('add_prize.html');
    }
    //修改奖品时段信息
    public function update_rule()
    {
        $id = $_GET['id'];
        $res = Model('long')->get_rule_one($id);
        $this->assign("res",$res);
        $prize_name = Model('long')->get_prize_name();
    
        $this->assign('prize_name',$prize_name);
        $this->display('add_rule.html');
    }
    //保存新奖品信息
    public function set_prize()
    {
        $message = array('error'=>1,'content'=>'');
        $id = $_POST['id'];
        $prize_name = $_POST['prize_name'];
            $mark = $_POST['mark'];
            $pby = $_POST['pby'];
            $card_type = $_POST['card_type'];
            if($pby<1 || $pby>100)
            {
                $message['content'] = '中奖概率填写错误';
                die(json_encode($message));
            }
            $pri = $_POST['pri'];
            if($pri<1)
            {
                $message['content'] = '中奖优先级填写错误';
                die(json_encode($message));
            }
            $card_id = $_POST['card_id'];
             
            $result = Model('long')->set_prize($id,$prize_name,$mark,$pby,$pri,$card_id,$card_type);
            if($result)
            {
                $message['error'] = 0;
                $message['content'] = '保存成功';
                die(json_encode($message));
            }else{
                $message['content'] = '操作失败，请重试！';
                die(json_encode($message));
            }
}
    //删除指定记录
    public function del_prize()
    {
        $id=isset($_REQUEST['id'])?$_REQUEST['id']:'';

        if(is_array($id))
        {
            foreach($id as $k=>$v)
            {
                $result=Model('long')->del_prize($v);
            }
        }
        else
        {
            $id=intval($id);

            $result=Model('long')->del_prize($id);
        }
        print_r(json_encode($result));
    }
     
    //删除指定记录
    public function del_rule()
    {
        $id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
         
        if(is_array($id))
        {
            foreach($id as $k=>$v)
            {
                $result=Model('long')->del_rule($v);
            }
        }
        else
        {
            $id=intval($id);
             
            $result=Model('long')->del_rule($id);
        }
        print_r(json_encode($result));
    }
     
    //保存新奖品时段信息
    public function set_rule()
    {
        $message = array('error'=>1,'content'=>'');
        $id = $_POST['id'];
        $pid = $_POST['pid'];
        $nums = $_POST['nums'];
        if($nums < 1)
        {
            $message['content'] = '奖品数量填写错误';
            die(json_encode($message));
        }
         
        $begintime = strtotime($_POST['begintime']);
        $endtime = strtotime($_POST['endtime']);
        if($begintime > $endtime)
        {
            $message['content'] = '开始时间必须早于结束时间';
            die(json_encode($message));
        }
         
        $result = Model('long')->set_rule($id,$pid,$nums,$begintime,$endtime);
        if($result)
        {
            $message['error'] = 0;
            $message['content'] = '保存成功';
            die(json_encode($message));
        }else{
            $message['content'] = '操作失败，请重试！';
            die(json_encode($message));
        }
    }
}   